<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\System;

use AuctionCore\Other\str2PY;

/**
 * Class Menu 菜单类
 * @package AuctionCore\System
 */
class Menu
{

    private $menu_id = null;


    public function __construct($id)
    {
        $this->menu_id = $id;

    }

    /**
     * @param $parent_id
     * @param $name
     * @param $model
     * @param $action
     * @param $extra
     * @param $rules
     * @return mixed
     */
    public static function create($parent_id, $name, $model, $action, $extra, $rules, $type)
    {
        $data = [
            'parent_id' => $parent_id,
            'name' => $name,
            'model' => $model,
            'action' => $action,
            'extra' => $extra,
            'rules' => $rules,
            'status' => 1,
            'type' => $type,
        ];

        return M('menu')->add($data);
    }

    /**
     * 获取菜单列表
     * @param int $parent_id 父菜单ID
     * @param int $status 菜单状态 1显示 0不显示
     * @return mixed
     */
    public static function menuList($parent_id = -1, $status = -1)
    {
        $map = [];
        if ($parent_id > -1) {
            $map['parent_id'] = $parent_id;
        }
        if($status > -1){
            $map['status'] = $status;
        }
        $menu_list = M("menu")->where($map)->order("listorder asc")->select();
        return $menu_list;
    }

    /**
     * @param $id
     * @return mixed
     */
    public static function detailById($id)
    {
        return M("menu")->where("id=" . $id)->find();
    }

    /**
     * @param $data
     * @return bool
     */
    public function update($data)
    {
        $map = ['id' => $this->menu_id];
        $res = M('menu')->where($map)->save($data);
        return $res;
    }

    public static function list_orders()
    {

        $ids = $_POST['listorders'];
        foreach ($ids as $key => $r) {
            $data['listorder'] = $r;
            M('menu')->where(['id' => $key])->save($data);
        }
        return true;
    }

    /**
     * @param $auth
     * @return array
     */
    public static function build_tree_menu($auth)
    {
        $tree_menu = [];
        $parent = Menu::menuList(0, 1);
        $py = new str2PY();
        // dump($parent);
        // dump($auth);
        foreach ($parent as $index => $item) {
            //当前父类的子类type为0，则该父类也需要显示出来
            $is_show = false;
            $childrenList = Menu::menuList($item['id'], 1);
            foreach ($childrenList as $k => $v) {
                if ($v['type'] == 0) {
                    $is_show = true;
                }
            }

            if (in_array(strtolower($item['rules']), $auth) || $is_show) {
                $children = [];
                // $childrenList = Menu::menuList($item['id']);
                foreach ($childrenList as $key => $val) {
                    if ($val['type'] == 0 || in_array(strtolower($val['rules']), $auth)) {
                        $children[] = [
                            'name' => $val['name'],
                            'url' => "/" . $val['model'] . "/" . $val['action'],
                            'first_char' => strtolower($py->getInitials($val['name']))
                        ];
                    };
                }

                $tree_menu[] = [
                    'name' => $item['name'],
                    'url' => "/" . $item['model'] . "/" . $item['action'],
                    'first_char' => strtolower($py->getInitials($item['name'])),
                    'children' => $children
                ];
            }
        }
        //dump($tree_menu[5]);
        return $tree_menu;
    }
}